package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.util.Log;
import android.view.View;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.contact.ForgetLoginPasswordContact;
import calorie.com.calorie.response.ResultDTO;
import retrofit2.Response;

public class ForgetLoginPasswordActivity extends MvpActivity<ForgetLoginPasswordContact.ForgetLoginPasswordPresent> implements ForgetLoginPasswordContact.IForgetLoginPasswordView {


    @BindView(R.id.b_forgetpassword_validate)
    AppCompatButton b_forgetpassword_validate;
    @BindView(R.id.et_forgetpassowrd_phone)
    AppCompatEditText et_forgetpassowrd_phone;
    @BindView(R.id.bt_forgetpassword_submit)
    AppCompatButton bt_forgetpassword_submit;

    @BindView(R.id.et_forgetpassowrd_account)
    AppCompatEditText et_forgetpassowrd_account;
    @BindView(R.id.et_forgetpassowrd_password)
    AppCompatEditText et_forgetpassowrd_password;
    @BindView(R.id.et_forgetpassowrd_confirm)
    AppCompatEditText et_forgetpassowrd_confirm;
    @BindView(R.id.et_forgetpassowrd_code)
    AppCompatEditText et_forgetpassowrd_code;
    private String TAG = "ForgetLoginPasswordActivity";
    @BindView(R.id.v_forgetpassword_back)
    public View v_forgetpassword_back;


    @Override
    protected ForgetLoginPasswordContact.ForgetLoginPasswordPresent createPresent() {
        return new ForgetLoginPasswordContact.ForgetLoginPasswordPresent();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_forgetloginpassword;
    }

    @Override
    public void getResult(Response<ResultDTO> response) {//提交数据回调
        String code = response.body().getCode();
       switch (code) {
           case  "200":
               ToastUtils.showLongToast(context, "密码修改成功,去登录");
               Intent intent = new Intent(context, LoginActivity.class);
               startActivity(intent);
               finish();
               break;
           case "201":
               ToastUtils.showLongToast(context, "服务器错误");
               break;
           case "203":
               ToastUtils.showLongToast(context, "验证码不正确");
               break;
           case "205":
               ToastUtils.showLongToast(context, "验证码已过期");
               break;
           case "206":
               ToastUtils.showLongToast(context, "用户不存在");
               break;
           case "207":
               ToastUtils.showLongToast(context, "手机号码错误");
               break;
       }



    }

    @Override
    public void getDataFailed() {

    }

    @Override
    public void getSendCodeResult(Response<ResultDTO> responseResponse) {//发送验证码回调
        Log.e(TAG, responseResponse.body().getCode());
        String code = responseResponse.body().getCode();
        switch (code) {
            case "200": //启动定时器
                ToastUtils.showLongToast(context, "验证码已发送");
                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(b_forgetpassword_validate, 60000, 1000);
                countDownTimerUtils.start();
                break;
            case "201":
                ToastUtils.showLongToast(context, "服务器错误");
                break;
            case "202":
                ToastUtils.showLongToast(context, "账号不存在");
                break;
            case "203":
                ToastUtils.showLongToast(context, "手机号码错误");
                break;
            case "204":
                ToastUtils.showLongToast(context, "账号错误");
                break;
        }



    }

    @OnClick({R.id.b_forgetpassword_validate, R.id.bt_forgetpassword_submit,R.id.v_forgetpassword_back})
    public void onViewClicked(View view) {
        String account = et_forgetpassowrd_account.getText().toString().trim();
        String phone = et_forgetpassowrd_phone.getText().toString().trim();
        String newLoginPassword_one = et_forgetpassowrd_password.getText().toString().trim();
        String newLoginPassword_two = et_forgetpassowrd_confirm.getText().toString().trim();
        String yzm = et_forgetpassowrd_code.getText().toString().trim();
        switch (view.getId()) {
            case R.id.b_forgetpassword_validate://获取验证码
                if (account.equals("")) {
                    ToastUtils.showLongToast(context, "请填写CALORIE账号");
                    return;
                }else if(phone.equals("")){
                    ToastUtils.showLongToast(context, "请填写手机号");
                    return;
                }
                mPresent.getCode(et_forgetpassowrd_phone.getText().toString().trim(),et_forgetpassowrd_account.getText().toString().trim());//点击发送验证码
                break;
            case R.id.bt_forgetpassword_submit://提交数据修改密码

                if (account.equals("")) {
                    ToastUtils.showLongToast(context, "请填写CALORIE账号");
                } else if (phone.equals("")) {
                    ToastUtils.showLongToast(context, "请填写手机号");
                } else if (newLoginPassword_one.equals("") || newLoginPassword_two.equals("")) {
                    ToastUtils.showLongToast(context, "请填写密码");
                } else if (!newLoginPassword_one.equals(newLoginPassword_two)) {
                    ToastUtils.showLongToast(context, "密码不一致");
                } else if (newLoginPassword_one.equals(newLoginPassword_two)) {
                    mPresent.forgetLoginPassword(account, phone, newLoginPassword_two, yzm);
                }
                break;
            case R.id.v_forgetpassword_back:
                this.finish();
                break;
        }
    }
}
